package com.lxl.todo;
//给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。

/**
 * url https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-liked
 * 移动零
 * 输入: nums = [0,1,0,3,12]
 * 输出: [1,3,12,0,0]
 */
public class Solution3 {
    public void moveZeroes(int[] nums) {
        int n = nums.length,left = 0,right = 0;
        while (right < n) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[right];
        nums[right] = nums[left];
        nums[left] = temp;
    }

    public static void main(String[] args) {
        int[] ints = {0, 1, 0, 3, 12};
        new Solution3().moveZeroes(ints);
        for (int i = 0; i < ints.length; i++) {
            System.out.println("i = " + ints[i]);
        }

    }

}
